{{- define "psi_monitor_resources" }}
cpu: 25m
memory: 25Mi
{{- end }}

{{- if .Values.nodeManager.earlyOomEnabled }}
  {{- if (.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: early-oom
  namespace: d8-cloud-instance-manager
  {{- include "helm_lib_module_labels" (list . (dict "app" "early-oom" "workload-resource-policy.deckhouse.io" "every-node")) | nindent 2 }}
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: DaemonSet
    name: early-oom
  updatePolicy:
    updateMode: "Auto"
  resourcePolicy:
    containerPolicies:
    - containerName: "psi-monitor"
      minAllowed:
        {{- include "psi_monitor_resources" . | nindent 8 }}
      maxAllowed:
        cpu: 50m
        memory: 50Mi
    {{- include "helm_lib_vpa_kube_rbac_proxy_resources" . | nindent 4 }}
  {{- end }}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: early-oom
  namespace: d8-cloud-instance-manager
  {{- include "helm_lib_module_labels" (list . (dict "app" "early-oom")) | nindent 2 }}
spec:
  updateStrategy:
    rollingUpdate:
      maxUnavailable: 1
    type: RollingUpdate
  selector:
    matchLabels:
      app: early-oom
  template:
    metadata:
      labels:
        app: early-oom
      name: early-oom
    spec:
      {{- include "helm_lib_priority_class" (tuple . "system-node-critical") | nindent 6 }}
      {{- include "helm_lib_tolerations" (tuple . "any-node") | nindent 6 }}
      {{- include "helm_lib_module_pod_security_context_run_as_user_root" . | nindent 6 }}
      serviceAccountName: early-oom
      containers:
      - name: psi-monitor
        image: {{ include "helm_lib_module_image" (list . "earlyOom") }}
        args:
        - --memory-threshold=30
        securityContext:
          privileged: true
        volumeMounts:
          - mountPath: /host_proc
            name: proc
        livenessProbe:
          httpGet:
            path: /healthz
            scheme: HTTPS
            port: 8443
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_only_logs" 10 | nindent 12 }}
  {{- if not ( .Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "psi_monitor_resources" . | nindent 12 }}
  {{- end }}
      - name: kube-rbac-proxy
        {{- include "helm_lib_module_container_security_context_read_only_root_filesystem" . | nindent 8 }}
        image: {{ include "helm_lib_module_common_image" (list . "kubeRbacProxy") }}
        args:
        - "--secure-listen-address=$(KUBE_RBAC_PROXY_LISTEN_ADDRESS):8443"
        - "--v=2"
        - "--logtostderr=true"
        - "--stale-cache-interval=1h30m"
        - "--livez-path=/livez"
        env:
        - name: KUBE_RBAC_PROXY_LISTEN_ADDRESS
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: KUBE_RBAC_PROXY_CONFIG
          value: |
            excludePaths:
            - /healthz
            upstreams:
            - upstream: http://127.0.0.1:8080/
              path: /
              authorization:
                resourceAttributes:
                  namespace: d8-cloud-instance-manager
                  apiGroup: apps
                  apiVersion: v1
                  resource: daemonsets
                  subresource: prometheus-metrics
                  name: early-oom
        ports:
        - containerPort: 8443
          name: https-metrics
        livenessProbe:
          httpGet:
            path: /livez
            port: 8443
            scheme: HTTPS
        readinessProbe:
          httpGet:
            path: /livez
            port: 8443
            scheme: HTTPS
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_only_logs" . | nindent 12 }}
  {{- if not ( .Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "helm_lib_container_kube_rbac_proxy_resources" . | nindent 12 }}
  {{- end }}
      volumes:
        - name: proc
          hostPath:
            path: /proc
            type: Directory
      imagePullSecrets:
      - name: deckhouse-registry
{{- end }}
